package Client.PokerClient.src;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
/**
 * Klasa okna serwer (lobby).
 * @author Artur Smolibowski
 */
public class ServerWindow {
  /**
   * Glowne okno programu.
   */
  private JFrame window;
  /**
   * Szerokosc okna.
   */
  private int windowWidth;
  /**
   * Dlugosc okna.
   */
  private int windowHeight;
  /**
   * Skroll do listy.
   */
  private JScrollPane scrServerList;
  /**
   * Lista wszystkich dostepnych gier.
   */
  private JList<String> lisServerList;
  /** Specjalan klasa do edycji wygladu okna. */
  private GridBagConstraints serverWindow;
  /** Przyciski do obslugi okna lobby. */
  private JButton[] butServerButton;
  /**
   * Konstruktor klasy.
   * @param window Glowne okno programu.
   * @param windowWidth Szerokosc okna.
   * @param windowHeight Dlugosc okna.
   * @param ServerDatabase Lista zawierjaca liste dostepnych gier.
   */
  ServerWindow(JFrame window, int windowWidth, int windowHeight, DefaultListModel<String> ServerDatabase) {
	serverWindow = new GridBagConstraints();

    butServerButton = new JButton[] { new JButton("JOIN"), new JButton("CREATE"), new JButton("REFRESH"), new JButton("DISCONECT") };
    lisServerList = new JList<String>(ServerDatabase);
    lisServerList.setBorder(BorderFactory.createLoweredBevelBorder());
    lisServerList.setVisibleRowCount(3);
    scrServerList = new JScrollPane(lisServerList);

    this.window=window;
    this.windowWidth=windowWidth;
    this.windowHeight=windowHeight;
  }
  /**
   * Dodanie ActionListener do klasy.
   * @param listener Action Listener klasy ServerWindow.
   */
  public void addActionListener(ServerListener listener) {
    for(int i=0; i<butServerButton.length; i++)
      butServerButton[i].addActionListener(listener);
  }
  /**
   * Edycja na okno serwera.
   */
  public void ChangeWindow() {
    window.getContentPane().removeAll();

    window.setSize(windowWidth, windowHeight);
    window.setLocationRelativeTo(null);

    window.setLayout(new GridBagLayout());
    serverWindow.fill = GridBagConstraints.VERTICAL;
    serverWindow.fill = GridBagConstraints.BOTH;

    serverWindow.weightx = 1;
    serverWindow.weighty = 1;
    serverWindow.gridwidth = 4;
    serverWindow.gridheight = 5;
    serverWindow.gridx = 0;
    serverWindow.gridy = 0;
    serverWindow.insets = new Insets(10,10,10,10);
    window.add(scrServerList, serverWindow);


    serverWindow.gridwidth = 1;
    serverWindow.gridheight = 1;
    for(int j=0; j<4; j++) {
      serverWindow.weightx = 0.25;
      serverWindow.weighty = 0.1;
      serverWindow.gridx = j;
      serverWindow.gridy = 5;
      window.add(butServerButton[j], serverWindow);
    }

    window.revalidate();
    window.repaint();
  }
  /**
   * Pobranie tablicy przyciskow lobby.
   * @return Tablica przyciskow lobby.
   */
  public JButton[] getServerButton() {
    return butServerButton;
  }
  /**
   * Pobranie listy zawierajacej liste dostepnych gier.
   * @return Lista zawierajaca liste dostepnych gier.
   */
  public JList<String> getServerList() {
    return lisServerList;
  }
}
